<template>
  <div>
    <!-- 搜索栏 -->
    <Row>
      <Card>
        <Form
          ref="search"
          :model="searchData"
          :rules="searchReg"
          :label-width="100"
        >
          <Row>
            <i-col :xs="24" :md="12" :lg="6">
              <FormItem label="搜索指标" prop="name">
                <Input
                  v-model="searchData.indexName"
                  suffix="ios-search"
                  placeholder="请输入指标名称"
                  style="width: auto"
                >
                  <Icon type="ios-search" slot="suffix" />
                </Input>
              </FormItem>
            </i-col>
            <i-col :xs="24" :md="12" :lg="6">
              <FormItem label="市考核责任单位" prop="name">
                <Input
                  v-model="searchData.cityResponUnit"
                  placeholder="请输入市考核责任单位"
                  style="width: auto"
                ></Input>
              </FormItem>
            </i-col>
            <i-col :xs="24" :md="12" :lg="6">
              <FormItem label="牵头单位" prop="name">
                <Select
                  filterable
                  clearable
                  placeholder="请输入牵头单位"
                  v-model="searchData.departCode"
                  style="width: 150px"
                >
                  <Option
                    v-for="item in getDepartmentList"
                    :value="item.departcode"
                    :key="item.departcode"
                    >{{ item.department }}</Option
                  >
                </Select>
              </FormItem>
            </i-col>
          </Row>
          <Row>
            <i-col :xs="24" :md="12" :lg="6">
              <FormItem label="年份">
                <DatePicker
                  type="year"
                  format="yyyy"
                  @on-change="searchSelectYear"
                  placeholder="按年份搜索"
                  style="width: 185px"
                  v-model="searchData.dateTime"
                ></DatePicker>
              </FormItem>
            </i-col>
          </Row>
          <FormItem>
            <Button
              v-if="btnAccess.query"
              type="primary"
              style="marigin: 0 20px"
              :loading="searchLoading"
              @click="searchSubmit(10, 1)"
              >提交搜索</Button
            >
            <Button @click="submitReset">重置搜索</Button>
          </FormItem>
        </Form>
      </Card>
    </Row>
    <!-- 导入excel -->
    <Row style="margin: 10px 0">
      <Card title="导入EXCEL">
        <Upload
          ref="upload"
          :before-upload="handleUpload2"
          :on-success="handleSuccess2"
          :format="['xls', 'xlsx']"
          :on-format-error="handleFormatError2"
          :action="importUrl"
        >
          <Button
            type="success"
            @click="downloadTemplate"
            style="margin-right: 20px"
            >下载专项工作督查指标模板</Button
          >
          <Tooltip
            content="温馨提示：上传文件前请先下载模板，依照模板格式上传"
            placement="top-start"
            max-width="220"
          >
            <Button
              type="primary"
              icon="ios-cloud-upload-outline"
              @click="exports"
              >导入专项工作督查指标</Button
            >
          </Tooltip>
        </Upload>

        <div v-if="file !== null">
          文件名: {{ file.name }}
          <Button type="primary" @click="upload" :disabled="isBanUpload"
            >点击上传</Button
          >
          <h3 style="color: #ff6600">{{ warnTips }}</h3>
          <div style="margin: 10px 0">
            导入批次号：{{ batchNum }}
            <!-- <DatePicker
              type="year"
              format="yyyy"
              v-model="indexsTime"
              @on-change="changeIndexTime"
              placeholder="请选择指标年份(必填)"
              style="width: auto;margin:0 10px;"
            ></DatePicker>-->
            <Tooltip
              content="温馨提示：确认上传前请重新校验所上传的数据！！"
              max-width="250"
              placement="top-start"
            >
              <Button
                type="success"
                @click="clickExport"
                :disabled="!isBanUpload"
                >确认导入</Button
              >
            </Tooltip>
          </div>
        </div>
        <!-- 校验表格回显 -->
        <Table
          v-if="isShowCheckTable"
          border
          :height="450"
          :columns="checkHead"
          :data="checkData"
          :row-class-name="rowClassName"
          :loading="checkTableLoading"
        ></Table>
      </Card>
    </Row>
    <!-- 表格 -->
    <Row style="margin-top: 20px">
      <Card>
        <div class="btn-arrs">
          <Button type="info" v-if="btnAccess.add" @click="showInsertModal"
            >添加工作督查</Button
          >
        </div>
        <Table
          border
          highlight-row
          :loading="tabelLoading"
          ref="currentRowTable"
          :columns="workDataTitle"
          :data="workDataContent"
          height="500"
        ></Table>
      </Card>
    </Row>

    <!-- 增加/编辑工作 -->
    <Modal @on-cancel="showState" v-model="isInsertModal" :title="modalTitle">
      <Form
        ref="work"
        :model="workForm"
        :rules="workFormReg"
        :label-width="150"
        inline
      >
        <FormItem label="年份" prop="dateTime">
          <DatePicker
            type="year"
            format="yyyy"
            @on-change="workSelectYear"
            placeholder="按年份搜索"
            style="width: 300px"
            :value="workForm.dateTime"
          ></DatePicker>
        </FormItem>
        <FormItem label="指标名称" prop="indexName">
          <Input
            v-model="workForm.indexName"
            placeholder="请输入指标名称"
            style="width: 300px"
          ></Input>
        </FormItem>
        <FormItem label="扣分上限" prop="maxPoint">
          <InputNumber
            :max="10"
            :min="0"
            :step="0.01"
            v-model="workForm.maxPoint"
            style="width: 300px"
          ></InputNumber>
        </FormItem>
        <FormItem label="匹配单位" prop="departCode">
          <Select
            filterable
            clearable
            placeholder="请选择匹配单位"
            v-model="workForm.departCode"
            style="width: 300px"
          >
            <Option
              v-for="item in getDepartmentList"
              :value="item.departcode"
              :key="item.departcode"
              >{{ item.department }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="部门" prop="branchs">
          <Select
            filterable
            clearable
            placeholder="请选择部门"
            v-model="workForm.branchs"
            style="width: 300px"
            @on-change="changBranch"
          >
            <Option
              v-for="item in branchList"
              :value="item.departAddr"
              :key="item.departAddr"
              >{{ item.departAddr }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="分管县领导" prop="leaderId ">
          <Select
            filterable
            clearable
            multiple
            placeholder="请选择分管县领导"
            v-model="workForm.leaderId"
            style="width: 300px"
          >
            <Option
              v-for="item in leaderList"
              :value="item.id + ''"
              :key="item.id"
              >{{ item.nickName }}</Option
            >
          </Select>
        </FormItem>

        <FormItem label="市考核责任单位" prop="cityResponUnit">
          <Input
            v-model="workForm.cityResponUnit"
            placeholder="请输入市考核责任单位"
            style="width: 300px"
          ></Input>
        </FormItem>
        <FormItem label="工作目标牵头单位" prop="leadUnit">
          <Input
            v-model="workForm.leadUnit"
            placeholder="请输入工作目标牵头单位"
            style="width: 300px"
          ></Input>
        </FormItem>
        <FormItem label="考核配合单位" prop="cooperateUnit">
          <Input
            v-model="workForm.cooperateUnit"
            placeholder="请输入考核配合单位"
            style="width: 300px"
          ></Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button
          v-if="btnAccess.upd"
          type="primary"
          :loading="submitloading"
          style="width: 120px"
          @click="workSubmit"
        >
          <span v-if="!submitloading">提交</span>
          <span v-else>提交中...</span>
        </Button>
      </div>
    </Modal>

    <!-- 导入检验模态框 -->
    <Modal
      v-model="checkModal"
      title="编辑"
      width="700px;"
      @on-cancel="closeCheckModal"
    >
      <Form
        ref="checkForm"
        :model="checkForm"
        :label-width="80"
        :rules="checkFormReg"
      >
        <FormItem label="指标年份" prop="cell1">
          <DatePicker
            type="year"
            format="yyyy"
            :value="checkForm.cell1"
            @on-change="handlerFormat"
            placeholder="请选择指标年份"
            style="width: 185px"
          ></DatePicker>
        </FormItem>
        <FormItem label="指标名称" prop="cell2">
          <Input
            disabled
            v-model="checkForm.cell2"
            placeholder="请输入指标名称"
          />
        </FormItem>
        <FormItem label="扣分上限" prop="cell3">
          <InputNumber
            :max="10"
            :min="0"
            :step="0.01"
            v-model="checkForm.cell3"
            placeholder="请输入扣分上限"
          ></InputNumber>
        </FormItem>
        <FormItem label="匹配单位" prop="cell23">
          <Select filterable clearable v-model="checkForm.cell23">
            <Option
              v-for="item in getDepartmentList"
              :value="item.departcode"
              :key="item.departcode"
              >{{ item.department }}</Option
            >
          </Select>
        </FormItem>
        <FormItem label="市考核责任单位" prop="cell4">
          <Input v-model="checkForm.cell4" placeholder="请输入市考核责任单位" />
        </FormItem>
        <FormItem label="工作目标牵头单位" prop="cell5">
          <Input
            v-model="checkForm.cell5"
            placeholder="请输入工作目标牵头单位"
          />
        </FormItem>
        <FormItem label="考核配合单位" prop="cell6">
          <Input v-model="checkForm.cell6" placeholder="请输入考核配合单位" />
        </FormItem>
        <FormItem label="备注">
          <Input
            disabled
            v-model="checkForm.remark"
            type="textarea"
            placeholder="请输入备注"
          />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button
          type="primary"
          style="width: 120px"
          @click="handleSave('checkForm')"
          :loading="loadingStatus"
          >{{ loadingStatus ? "提交中..." : "提交" }}</Button
        >
        <Button @click="closeCheckModal">取消</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { URL } from "@/libs/util";
import { workAjax } from "@/api/workSupervision";
// 以防命名冲突
const HOST = URL;
// 模态框TITLE
const INSERT_TITLE = "添加工作督查指标";
const UPDATE_TITLE = "编辑工作督查指标";
// 添加URL
const INSERT_URL = "/workSupervision/insert";
// 查询URL
const QUERY_URL = "/workSupervision/query";
// 修改URL
const UPDATE_URL = "/workSupervision/update";
// 删除RL
const DELETE_URL = "/workSupervision/delete";
// 以下为导入校验
// 查询校验
const QUERYCHECK_URL = "/workSupervision/queryImportDTO";
// 修改保存
const UPDEXCEL_URL = "/workSupervision/updateExcelDto";
// 确认导入接口
const CONFIRM_URL = "/workSupervision/saveWorkIndex";
export default {
  data() {
    return {
      // 增删改查权限按钮
      btnAccess: {
        add: true,
        add2: "inline-block",
        del: "inline-block",
        upd: true,
        query: true,
      },
      host: "", // 上传地址
      modalTitle: "", // 模态框title
      submitloading: false, // 提交loading
      searchLoading: false, // 搜索loading
      isInsertModal: false, // 模态框
      tabelLoading: false, // 表格loading
      pageSize: 10,
      pageNumber: 1,
      pageTotal: 10,
      searchData: {
        cityResponUnit: "", // 责任单位
        departCode: "", // 牵头单位
        indexName: "", // 指标名称
        dateTime: "", // 年份
      },
      workForm: {
        leadUnit: "", // 牵头单位
        cityResponUnit: "", // 责任单位
        cooperateUnits: [], // 配合单位
        cooperateUnit: "", // 考核配合单位
        departCode: "", // 匹配单位
        indexName: "", // 指标名称
        dateTime: "", // 年份
        maxPoint: null, // 分数上线
        leaderName: "", // 分管县领导
        branchs: "", // 部门
        leaderId: [], // 分管县领导 多选
      },
      leaderList: [],
      branchList: [],
      searchReg: {},
      workFormReg: {
        indexName: [
          {
            required: true,
            message: "请输入指标名称",
            trigger: "blur",
          },
        ],
        cityResponUnit: [
          {
            required: true,
            message: "请输入责任单位",
            trigger: "blur",
          },
        ],
        leadUnit: [
          {
            required: true,
            message: "请输入工作目标牵头单位",
            trigger: "blur",
          },
        ],
        cooperateUnit: [
          {
            required: true,
            message: "请输入考核配合单位",
            trigger: "blur",
          },
        ],
        departCode: [
          {
            required: true,
            message: "请输入牵头单位",
            trigger: "change",
          },
        ],
        maxPoint: [
          {
            required: true,
            message: "请输入扣分上限值",
          },
        ],
        dateTime: [
          {
            required: true,
            message: "请选择年份",
            trigger: "blur",
          },
        ],
      },
      workDataTitle: [
        {
          title: "序号",
          type: "index",
          width: 60,
          align: "center",
        },
        {
          title: "年份",
          key: "dateTime",
          width: 100,
        },
        {
          title: "指标名称",
          key: "indexName",
          minWidth: 130,
        },
        {
          title: "扣分上限",
          key: "maxPoint",
          width: 110,
          align: "center",
        },
        {
          title: "匹配单位",
          key: "departAddr",
        },
        {
          title: "分管县领导",
          key: "leaderName",
          render: (h, params) => {
            let text = "";
            let list = params.row.leaderId;
            let branch = this.branchList;
            // 循环回显表格分管领导字段
            for (let i = 0; i < branch.length; i++) {
              for (let j = 0; j < branch[i].userlists.length; j++) {
                let item = branch[i].userlists;
                for (let p = 0; p < list.length; p++) {
                  if (list[p] + "" === item[j].id + "") {
                    text += item[j].nickName + ",";
                  }
                }
              }
            }
            return h("div", {}, text);
          },
        },
        {
          title: "市考核责任单位",
          key: "cityResponUnit",
        },
        {
          title: "工作目标牵头单位",
          key: "leadUnit",
        },
        {
          title: "考核配合单位",
          key: "cooperateUnit",
        },
        {
          title: "操作",
          key: "action",
          width: 230,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.modalTitle = UPDATE_TITLE;
                      this.workForm = JSON.parse(JSON.stringify(params.row));
                      this.workForm.maxPoint = Math.abs(this.workForm.maxPoint);
                      console.log(this.workForm);
                      // 赋值弹窗部门值
                      let arr = this.workForm.leaderId;
                      let branch = this.branchList;
                      if (arr === []) {
                        console.log(this.leaderList);
                        this.leaderList = data[0].userlists; // 领导数组
                      } else {
                        for (let i = 0; i < branch.length; i++) {
                          for (let j = 0; j < branch[i].userlists.length; j++) {
                            let item = branch[i].userlists;
                            if (Number(arr[0]) === item[j].id) {
                              console.log(item[j]);
                              this.leaderList = item;
                              this.workForm.leaderId = params.row.leaderId;
                              this.workForm.branchs = branch[i].departAddr;
                            }
                          }
                        }
                      }

                      this.isInsertModal = true;
                    },
                  },
                },
                "查看或编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                  },
                  style: {
                    display: this.btnAccess.del,
                  },
                  on: {
                    click: () => {
                      this.workRemove(params.row);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
      workDataContent: [],
      /** ******上传下载数据**************************************** */
      // 上传表格
      uploadLoading: false,
      progressPercent: 0,
      showProgress: false,
      showRemoveFile: false,
      file: null,
      tableData: [], // 上传Excel 内容
      tableTitle: [], //
      excelTime: "",
      tableLoading: false,
      UploadLoadingBtn: false,
      // 导入表格回显
      indexsTime: "",
      batchNum: "", // 导入批次
      isBanUpload: false,
      isShowCheckTable: false,
      warnTips: "", // 警告提示
      // 校验模态框
      checkModal: false,
      checkForm: {
        cell1: "", // 年份
        cell2: "", // 指标名称
        cell3: 0, // 扣分上限
        cell4: "", // 市考核责任单位
        cell5: "", // 工作目标牵头单位
        cell6: "", // 考核配合单位
        cell23: "", // 配合单位
        remark: "", // 备注
      },
      checkFormReg: {
        cell1: [
          {
            required: true,
            type: "string",
            message: "请选择年份",
            trigger: "change",
          },
        ],
        cell2: [
          {
            required: true,
            message: "请输入指标名称",
            trigger: "blur",
          },
        ],
        // cell3: [
        //   {
        //     required: true,
        //     type: "string",
        //     message: "请输入扣分上限值",
        //     trigger: "change"
        //   }
        // ],
        cell4: [
          {
            required: true,
            message: "请输入市考核责任单位",
            trigger: "blur",
          },
        ],
        cell5: [
          {
            required: true,
            message: "请输入工作目标牵头单位",
            trigger: "blur",
          },
        ],
        cell6: [
          {
            required: true,
            message: "请输入考核配合单位",
            trigger: "blur",
          },
        ],
        cell23: [
          {
            required: true,
            message: "请选择配合单位",
            trigger: "change",
          },
        ],
      },
      // 导入回显表格
      checkTableLoading: false,
      checkHead: [
        {
          title: "序号",
          key: "cell0",
          width: 50,
        },
        {
          title: "年份",
          key: "cell1",
          width: 80,
        },
        {
          title: "指标名称",
          key: "cell2",
          width: 200,
        },
        {
          title: "扣分上限",
          key: "cell3",
          width: 150,
        },
        {
          title: "备注",
          key: "remark",
          width: 230,
        },
        {
          title: "市考核责任单位",
          key: "cell4",
          width: 150,
        },
        {
          title: "工作目标牵头单位",
          key: "cell5",
          width: 150,
        },
        {
          title: "考核配合单位",
          key: "cell6",
          width: 150,
        },
        {
          title: "操作",
          key: "action",
          width: 100,
          fixed: "right",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "large",
                  },
                  on: {
                    click: () => {
                      this.checkForm = JSON.parse(JSON.stringify(params.row));
                      // this.checkForm.cell1 = parseInt(this.checkForm.cell1);
                      this.checkModal = true;
                    },
                  },
                },
                "编辑"
              ),
            ]);
          },
        },
      ],
      checkData: [],
      importUrl: URL + "/workSupervision/importPoint",
      loadingStatus: false,
      lastIndex: [], // 上级指标
    };
  },
  methods: {
    showState() {
      this.$refs["work"].resetFields();
    },
    // 重置搜索内容
    submitReset() {
      this.searchData = {
        cityResponUnit: "", // 责任单位
        departCode: "", // 牵头单位
        indexName: "", // 指标名称
        dateTime: "", // 年份
      };
      this.searchLoading = false;
    },
    // 搜索提交
    searchSubmit(pageSize, pageNumber) {
      this.pageSize = pageSize;
      this.pageNumber = pageNumber;
      this.searchLoading = true;
      this._getWorkeData(
        this.searchData,
        QUERY_URL,
        this.pageSize,
        this.pageNumber
      );
    },
    // 工作督查指标提交
    workSubmit() {
      this.$refs["work"].validate((valid) => {
        if (valid) {
          this.submitloading = true;
          // 添加工作督查指标
          if (this.modalTitle === INSERT_TITLE) {
            this._workAjax(INSERT_URL, this.workForm).then((result) => {
              this.submitloading = false;
              if (result.code === "200") {
                this.isInsertModal = false;
                this.$Message.success("添加成功");
                // 添加成功 重新查询
                this._getWorkeData(
                  this.searchData,
                  QUERY_URL,
                  this.pageSize,
                  this.pageNumber
                );
              } else {
                this.$Message.error("添加失败！请刷新页面后尝试");
              }
            });
            // 编辑工作督查指标
          } else if (this.modalTitle === UPDATE_TITLE) {
            this._workAjax(UPDATE_URL, this.workForm).then((result) => {
              this.submitloading = false;
              if (result.code === "200") {
                this.isInsertModal = false;
                this.$Message.success("修改成功");
                // 修改成功 重新查询
                this._getWorkeData(
                  this.searchData,
                  QUERY_URL,
                  this.pageSize,
                  this.pageNumber
                );
              } else {
                this.$Message.error("修改失败！请刷新页面后尝试");
              }
            });
          }
        } else {
          this.$Message.error("带*为必填项");
        }
      });
    },
    // 页码
    pageNumberChange(pageNumber) {
      this.pageNumber = pageNumber;
      this._getWorkeData(
        this.searchData,
        QUERY_URL,
        this.pageSize,
        this.pageNumber
      );
    },
    // 页数
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this._getWorkeData(
        this.searchData,
        QUERY_URL,
        this.pageSize,
        this.pageNumber
      );
    },
    // 删除工作督查指标
    workRemove(params) {
      this.$Modal.confirm({
        title: "删除新闻",
        content: "<p>删除后将无法恢复</p>",
        loading: true,
        onOk: () => {
          this._workAjax(DELETE_URL, params).then((result) => {
            if (result.code === "200") {
              this.$Message.success("删除成功");
              this.$Modal.remove();
              // 删除成功 重新查询
              this._getWorkeData(
                this.searchData,
                QUERY_URL,
                this.pageSize,
                this.pageNumber
              );
            } else {
              this.$Message.error("删除失败！请刷新页面重试");
            }
          });
        },
      });
    },
    // 搜索年份时间转换
    searchSelectYear(year) {
      this.searchData.dateTime = year;
    },
    // 工作督查指标年份
    workSelectYear(year) {
      this.workForm.dateTime = year;
    },
    // 模态框
    showInsertModal() {
      this.$refs["work"].resetFields();
      this.isInsertModal = true;
      // 改变模态框title
      this.modalTitle = INSERT_TITLE;
    },
    // 添加编辑AJAX
    _workAjax(url, formData) {
      const keyOne = "workSupervisionEntity";
      return new Promise((resolve, reject) => {
        workAjax({ url, formData, keyOne })
          .then((result) => {
            if (result.data !== undefined) {
              resolve(result.data);
            } else {
              reject(err);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      });
    },
    /** 查询校验表格 **************************** ******************/
    _queryCheckAjax(form, url, pageNumber, pageSize) {
      const keyOne = "excelDTO";
      let formData = Object.assign(form, {
        pageSize: pageSize,
        pageNumber: pageNumber,
      });
      return new Promise((resolve, reject) => {
        workAjax({ formData, url, keyOne }).then((res) => {
          if (res.data.code === "200") {
            resolve(res.data);
          } else {
            reject(error);
          }
        });
      });
    },
    // 获取新闻
    _getWorkeData(formData, url, pageSize, pageNumber) {
      this.tabelLoading = true;
      formData = Object.assign(formData, {
        pageSize,
        pageNumber,
      });
      const keyOne = "workSupervisionFilter";
      workAjax({ formData, url, keyOne })
        .then((result) => {
          this.tabelLoading = false;
          if (result.data.code === "200") {
            this.searchLoading = false;
            this.workDataContent = result.data.results.list;
            for (let i = 0; i < this.workDataContent.length; i++) {
              this.workDataContent[i].maxPoint = Math.abs(this.workDataContent[i].maxPoint)
            }
            this.pageTotal = parseInt(result.data.results.pageTotal) * 10;
            this.$Message.success("查询成功");
          } else {
            this.$Message.error("查询失败");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    /**
     * 下载工作督查模板
     */
    downloadTemplate() {
      window.open(
        HOST +
          "/townScore/downTownScoreMoudle?fileName=workDuChaMould.xls&chinaName=专项工作督查指标导入模板.xls"
      );
    },
    /****************************************************************/
    // 查询校验表格
    queryCheckTable() {
      let formData = {
        batchNum: this.batchNum,
      };
      this.checkTableLoading = true;
      this._queryCheckAjax(formData, QUERYCHECK_URL).then((ressult) => {
        if (ressult.code === "200") {
          this.checkData = ressult.results.list;
          this.isBanUpload = true;
          this.checkTableLoading = false;
          this.$Message.success(ressult.message);
          this.warnTips = ressult.results.warn;
        } else {
          this.$Message.error(ressult.message);
        }
      });
    },
    // 导入excel
    // 阻止自动上传文件
    handlerFormat(year) {
      this.checkForm.cell1 = year;
    },
    handleUpload2(file) {
      this.file = file;
      return false;
    },
    // 上传文件
    handleSuccess2(res, file) {
      if (res.code === "200") {
        this.batchNum = res.results.batchNum;
        this.$Notice.success({
          title: res.message,
        });
        let formData = {
          batchNum: this.batchNum,
        };
        // 回显校验表格
        this.checkTableLoading = true;
        this._queryCheckAjax(formData, QUERYCHECK_URL).then((ressult) => {
          if (ressult.code === "200") {
            this.checkData = ressult.results.list;
            this.isBanUpload = true;
            this.checkTableLoading = false;
            this.$Message.success(ressult.message);
            this.warnTips = ressult.results.warn;
          } else {
            this.$Message.error(ressult.message);
          }
        });
      } else if (res.code === "500") {
        this.$Notice.error({
          title: res.message,
        });
      }
    },
    // 点击上传文件
    upload() {
      this.$refs.upload.post(this.file); // 请求
      this.isShowCheckTable = true;
    },
    // 验证上传格式
    handleFormatError2(file) {
      this.$Notice.warning({
        title: "文件格式错误",
        desc: "文件" + file.name + " 格式错误, 请选择xls或者xlsx",
      });
    },
    // 最终确认认导入
    clickExport() {
      this.$Modal.confirm({
        width: 600,
        title: "是否确认导入",
        content:
          "<p>如有橙黄警告信息数据，该条数据将不会被保存，是否确认导入？</p>" +
          "<p>" +
          this.warnTips +
          "</p>",
        onOk: () => {
          this.confirmImport();
        },
        onCancel: () => {},
      });
    },
    // 确认导入
    confirmImport() {
      let formData = {
        batchNum: this.batchNum,
      };
      this._queryCheckAjax(formData, CONFIRM_URL).then((res) => {
        if (res.code === "200") {
          this.$Message.success(res.message);
          // 重新查询表格
          this._getWorkeData(
            this.searchData,
            QUERY_URL,
            this.pageSize,
            this.pageNumber
          );
        } else {
          this.$Message.error(res.message);
        }
        this.isShowCheckTable = false;
        // 清除数据
        setTimeout(() => {
          this.isBanUpload = false;
          this.warnTips = "";
          this.file = null;
        }, 500);
      });
    },
    // 修改保存
    handleSave(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.loadingStatus = true;
          let formData = this.checkForm;
          this._queryCheckAjax(formData, UPDEXCEL_URL).then((res) => {
            // this.checkTableLoading = true;
            this.checkModal = false;
            if (res.code === "200") {
              // 重新查询
              this.queryCheckTable();
              this.loadingStatus = false;
              this.$Message.success(res.message);
            } else {
              this.$Message.error(res.message);
            }
          });
        } else {
          this.$Message.error("带*为必填项");
        }
      });
    },
    selectTestType(val) {
      let arr = this.$store.state.user.departmentList; // 获取单位数组
      for (let i = 0; i < arr.length; i++) {
        if (val === arr[i].departcode) {
          return arr[i].department;
        }
      }
    },
    /**
     * 校验表格时判断是否有误显示红色
     * @param {row} 表格行数据
     */
    rowClassName(row, index) {
      if (row.avail === 0) {
        return "demo-table-error-row";
      }
      return "";
    },
    exports() {
      this.isBanUpload = false;
    },
    closeCheckModal() {
      this.checkModal = false;
    },
    // 获取分管县领导和部门
    _getleadersList(form) {
      const url = "/app/sys/getManage";
      let formData = {};
      return new Promise((resolve, reject) => {
        workAjax({ formData, url }).then((res) => {
          if (res.data !== undefined) {
            resolve(res.data);
          } else {
            reject();
          }
        });
      });
    },
    // 改变部门时获取数据
    changBranch() {
      let branch = this.workForm.branchs;
      this.branchList.forEach((item) => {
        if (item.departAddr === branch) {
          console.log(item.userlists);
          this.leaderList = item.userlists;
        }
      });
      this.workForm.leaderId = []; // 切换部门时清空领导列表
    },
  },
  computed: {
    getDepartmentList() {
      return this.$store.state.user.departmentList;
    },
  },
  created() {
    this._getWorkeData(
      this.searchData,
      QUERY_URL,
      this.pageSize,
      this.pageNumber
    );
    // 查询分管县领导联动下拉数据
    this._getleadersList({}).then((res) => {
      console.log(res);
      if (res.code === "200") {
        var data = res.results.data;
        this.branchList = res.results.data; // 赋值
        if (data[0] !== undefined) {
          this.leaderList = data[0].userlists; // 领导数组
          this.workForm.branchs = data[0].departAddr; // 部门
        }
      }
    });

    // 获取按钮权限
    let btnAccess = sessionStorage.getItem("btnaccess");
    this.globalFunction(btnAccess);
  },
};
</script>
<style>
.ivu-table .demo-table-error-row td {
  background-color: #ff6600;
  color: #fff;
}
</style>
